package cn.gjculture.shopping.api.service.impl;

import cn.gjculture.shopping.api.dao.*;
import cn.gjculture.shopping.api.entity.Commodity;
import cn.gjculture.shopping.api.entity.Hotel;
import cn.gjculture.shopping.api.entity.House;
import cn.gjculture.shopping.api.entity.Return;
import cn.gjculture.shopping.api.service.GoodsService;
import cn.gjculture.shopping.api.service.HotelService;
import cn.gjculture.shopping.api.util.DateUtil;
import cn.gjculture.shopping.api.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HotelServiceImpl implements HotelService {

    @Autowired
    HotelDao hotelDao;

    @Autowired
    CommonUpdateMapper commonUpdateMapper;

    @Autowired
    CommonDeleteMapper commonDeleteMapper;

    @Autowired
    HouseDao houseDao;

    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    GoodsDao goodsDao;

    @Autowired
    GoodsService goodsService;

    @Override
    public int insert(Hotel hotel) {
        hotel.setHotelId(UUIDUtil.getUUID());
        hotel.setInsertTime(DateUtil.getTimestamp());
        hotel.setUpdateTime(DateUtil.getTimestamp());
        hotel.setStatus(0);
        goodsService.insertThumbnail(hotel.getThumbnails(),hotel.getHotelId());
        int i=hotelDao.insert(hotel);
        return i;
    }

    @Override
    public List<Hotel> query() {
        List<Hotel> hotels=hotelDao.query();
        for(Hotel hotel:hotels){
            this.collect(hotel);
        }
        return hotels;
    }

    private void collect(Hotel hotel) {
        List<Commodity.Thumbnail> thumbnails=goodsDao.queryThumb(hotel.getHotelId());
        hotel.setThumbnails(thumbnails);
    }

    @Override
    public Hotel queryById(String hotelId) {
        Hotel hotels=hotelDao.queryById(hotelId);
        return hotels;
    }

    @Override
    public int update(Hotel hotel) {
        hotel.setUpdateTime(DateUtil.getTimestamp());
        goodsService.handleThumb(hotel.getHotelId(),hotel.getThumbnails());
        return commonUpdateMapper.updateHotel(hotel);
    }


    @Override
    public int delete(List<String> hotelIds, Return<Hotel> ret) {
        for (String id:hotelIds){
            List<House> houses=houseDao.queryByHotel(id);
            if(houses!=null&&houses.size()>0){
                ret.getCommon().setMessage("请先删除酒店下面的房型再删除酒店");
                return 0;
            }
        }
        return commonDeleteMapper.deleteHotel(hotelIds);
    }

    @Override
    public int recommended(Hotel hotel) {
        return hotelDao.recommended(hotel);
    }

    @Override
    public List<Hotel> queryRecommended() {
        return hotelDao.queryRecommended();
    }

}
